<?php

namespace Newadmin\Controller\Widget;

use Think\Controller;

class VoteController extends \Newadmin\Controller\Common\CommonController {

    public function _initialize() {
        parent::_initialize();
        $this->assign("module_name", "投票");

        $this->assign("first_title", "投票");
    }

    /*
     * 投票的主题列表
     */

    public function topics() {
        $this->assign("second_title", "主题列表");
        if (IS_AJAX) {
            $DB1 = M('widget_vote_products');
            $DB2 = M('widget_vote_records');
            $vote_topics_res = M("widget_vote_topics")->select();
            if(!empty($vote_topics_res)){
                foreach ($vote_topics_res as $key => $value) {
                    $vote_topics_res[$key]['btime'] = date('Y-m-d',$value['btime']);
                    $vote_topics_res[$key]['etime'] = date('Y-m-d',$value['etime']);
                    $vote_topics_res[$key]['pcounts'] = $DB1->where(array('vid' => $value['vid'],'ishidden' => 0))->count();
                    $vote_topics_res[$key]['vcounts'] = $DB2->where(array('vid' => $value['vid']))->count();
                    $sql = "select count(*) as mcount from (select distinct mid from fd_widget_vote_records where vid=".$value['vid'].") as newt1";
                    $turnout = $DB2->query($sql);
                    $vote_topics_res[$key]['turnout'] = $turnout[0]['mcount'];
                }
            }
            echo json_encode($vote_topics_res);
        } else {
            $this->display();
        }
    }

    /*
     * 主题增加
     */

    public function topic_add() {
        $this->assign("second_title", "主题添加");
        if (IS_POST) {
            $data['vtitle'] = trim(I('post.vtitle'));
            $data['vtop_img'] = '/Uploads'.'/'.$this->upload($_FILES['vtop_img']);
            $data['max_num'] = trim(I('post.max_num'));
            $data['repeat_num'] = trim(I('post.repeat_num'));
            $btime = I('post.btime');
            $data['btime'] = strtotime($btime);
            $etime = I('post.etime');
            $data['etime'] = strtotime($etime);
            $data['rules'] = I('post.rules');
            $data['ctime'] = NOW_TIME;
            $data['remarks'] = trim(I('post.remarks'));
            $res = M('widget_vote_topics')->data($data)->add();
            if(!empty($res)){
                $this->success('添加成功',U('Newadmin/Widget/Vote/topics'));
            }else{
                $this->error('添加失败');
            }
        } else {
            $this->display();
        }
    }

    /*
     * 主题删除
     */

    public function topic_del() {
        
    }

    /*
     * 主题修改
     */

    public function topic_modify() {
        $this->assign("second_title", "主题修改");
        $DB = M('widget_vote_topics');
        if (IS_POST) {
            //提交数据
            $data['vtitle'] = trim(I('post.vtitle'));
            if(!empty($_FILES['vtop_img']['size'])){
                $data['vtop_img'] = '/Uploads'.'/'.$this->upload($_FILES['vtop_img']);
            }
            $data['max_num'] = trim(I('post.max_num'));
            $data['repeat_num'] = trim(I('post.repeat_num'));
            $map['vid'] = I('post.vid');
            $btime = I('post.btime');
            $data['btime'] = strtotime($btime);
            $etime = I('post.etime');
            $data['etime'] = strtotime($etime);
            $data['rules'] = I('post.rules');
            $data['remarks'] = trim(I('post.remarks'));
            $res = M('widget_vote_topics')->where($map)->data($data)->save();
            if(!empty($res)){
                $this->success('修改成功',U('Newadmin/Widget/Vote/topics'));
            }else{
                $this->error('修改失败');
            }
        } else {
            $vid = I('get.vid');
            $res = $DB->where(array('vid' => $vid))->find();
            $res['btime'] = date('Y-m-d',$res['btime']);
            $res['etime'] = date('Y-m-d',$res['etime']);
            $res['rules'] = htmlspecialchars_decode($res['rules']);
            $this->assign('res',$res);
            $this->display();
        }
    }

    /*
     * 主题隐藏或打开
     */

    public function topic_chg_status() {
        
    }

    /*
     * 产品列表
     */

    public function products() {
        $this->assign("second_title", "列表");
        $vid = I('get.topic');
        $vtitle = M('widget_vote_topics')->where(array('vid' => $vid))->getField('vtitle');
        $DB = M('widget_vote_products');
        if (IS_AJAX) {
            if(!empty($vid)){
                $list = $DB->where(array('vid' => $vid,'ishidden' => 0))->select();
                foreach ($list as $key => $value) {
                    $list[$key]['vtitle'] = $vtitle;
                    $list[$key]['ctime'] = date('Y-m-d H:i:s',$value['ctime']);
                    $sql = "select count(*) as rank from (select * from fd_widget_vote_products where votes>=".$value['votes']." and ishidden=0 and vid=".$vid." order by votes asc) as newt2";
                    $rank = $DB->query($sql);
                    $list[$key]['rank'] = $rank[0]['rank'];
                }

            }else{
                $list = array();
            }
            echo json_encode($list);
        } else {
            $res = M('widget_vote_topics')->field('vid,vtitle')->select();
            $this->assign('res',$res);
            $this->display();
        }
    }

    /*
     * 产品添加
     */

    public function product_add() {
        $this->assign("second_title", "产品添加");
        $DB = M('widget_vote_products');
        if (IS_POST) {
            $data['vid'] = I('post.vid');
            $data['brand'] = trim(I('post.brand'));
            $data['thumb'] = '/Uploads'.'/'.$this->upload($_FILES['thumb']);
            $data['name'] = trim(I('post.name'));
            $data['spec'] = trim(I('post.spec'));
            $data['factory'] = trim(I('post.factory'));
            $data['slogan'] = trim(I('post.slogan'));
            $data['vote_num'] = trim(I('post.vote_num'));
            $data['ctime'] = NOW_TIME;
            $res = $DB->data($data)->add();
            if(!empty($res)){
                $this->success('添加成功',U('Newadmin/Widget/Vote/products'));
            }else{
                $this->error('添加失败');
            }
        } else {
            $res = M('widget_vote_topics')->field('vid,vtitle')->select();
            $this->assign('res',$res);
            $this->display();
        }
    }

    /*
     * 产品删除
     */

    public function product_del() {
        
    }

    /*
     * 产品隐藏
     */

    public function product_chg_status() {
        
    }

    /*
     * 产品修改
     */

    public function product_modify() {
        $this->assign("second_title", "产品修改");
        $DB = M('widget_vote_products');
        if (IS_POST) {
            $map['pid'] = I('post.pid');
            $data['brand'] = trim(I('post.brand'));
            if(!empty($_FILES['thumb']['size'])){
                $data['thumb'] = '/Uploads'.'/'.$this->upload($_FILES['thumb']);
            }
            $data['name'] = trim(I('post.name'));
            $data['spec'] = trim(I('post.spec'));
            $data['factory'] = trim(I('post.factory'));
            $data['slogan'] = trim(I('post.slogan'));
            $data['vote_num'] = trim(I('post.vote_num'));
            $res = $DB->where($map)->data($data)->save();
            if(!empty($res)){
                $this->success('修改成功',U('Newadmin/Widget/Vote/products'));
            }else{
                $this->error('修改失败');
            }
        } else {
            $pid = I('get.pid');
            $list = $DB->where(array('pid' => $pid))->find();
            $this->assign('list',$list);
            $this->display();
        }
    }


    /**
     * 删除产品
     * @return [type] [description]
     */
    public function product_delete(){
        $map['pid'] = I('get.pid');
        $DB = M('widget_vote_products');
        $ret = $DB->where($map)->data(array('ishidden' => 1))->save();
        if(!empty($ret)){
            $res['err'] = 0;
            $res['msg'] = '删除成功';
        }else{
            $res['err'] = 1;
            $res['msg'] = '删除失败';
        }
        echo json_encode($res);
    }

    
    /*
     * 
     * 
     * ########################   统计记录  ####################
     * 
     * 
     * 
     */
    
    /*
     * 投票记录
     */
    public function vote_records() {
        if(IS_AJAX)
        {
            $pid = I("get.pid");
            $DB = M("widget_vote_records");
            
            $res = $DB->field("fd_widget_vote_records.*,fd_members.username as votename")
                    ->join("fd_members on fd_members.id =fd_widget_vote_records.mid")
                    ->where("fd_widget_vote_records.pid = $pid")
                    ->order("fd_widget_vote_records.cdate desc")
                    ->select();
            
            echo json_encode($res);
        }
        else
        {
            $pid = I("get.pid");
            $this->assign("pid", $pid);
            $this->display();
        }
        
        
        
    } 
    


    //文件上传累
    private function upload($file){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     0 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath  =      ''; // 设置附件上传目录
        // 上传文件 
        $info   =   $upload->uploadOne($file);
        if(!$info)
        {
        // 上传错误提示错误信息    
                $this->error($upload->getError());
        }
        else
        {
                // 上传成功 获取上传文件信息    
                return $info['savepath'].$info['savename'];    
        }
    }

}
